# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# Name:         customer
# Description:  
# Author:       Administrator
# Date:         2020/2/10
#-------------------------------------------------------------------------------
from guitest.woniusales_test01.util.utility import Utility
from selenium import webdriver

class Customer:

	# 输入电话号码
	def input_phone(self,driver,customerphone):
		phone = driver.find_element_by_id('customerphone')
		Utility.send_input(phone,customerphone)

	# 输入会员昵称
	def input_name(self,driver,customername):
		cname = driver.find_element_by_id('customername')
		Utility.send_input(cname,customername)

	# 随机选择孩子的性别
	def select_sex(self,driver):
		sex_select = driver.find_element_by_id('childsex')
		Utility.select_random(sex_select)

	# 向出生日期输入框输入出生日期
	def input_date(self,driver,childdate):
		Utility.remove_readonly(driver,'childdate')
		childdate_ele = driver.find_element_by_id('childdate')
		Utility.send_input(childdate_ele,childdate)

	# 输入母婴积分
	def input_creditkids(self,driver,creditkids):
		kids = driver.find_element_by_id('creditkids')
		Utility.send_input(kids,creditkids)

	# 输入童装积分
	def input_creditcloth(self,driver,creditcloth):
		cloth = driver.find_element_by_id('creditcloth')
		Utility.send_input(cloth,creditcloth)

	# 点击添加按钮
	def click_add_button(self,driver):
		driver.find_element_by_css_selector('button.form-control:nth-child(1)').click()

	# 点击查询按钮:由于只有电话号码是查询条件，所以需要输入电话号码后再点击查询按钮
	def click_query_button(self,driver,customerphone):
		self.input_phone(driver,customerphone)
		driver.find_element_by_css_selector('button.form-control:nth-child(3)').click()

	# xpath:/html/body/div[4]/div[2]/table/tbody/tr[8]/td[11]/a
	# //tbody[@id="customerlist"]/tr[8]/td[11]/a
	# 随机获取列表中的修改链接元素
	def get_random_edit_ele(self,driver,customer_count):
		import random
		driver.find_element_by_xpath('//tbody[@id="customerlist"]/tr[%d]/td[11]/a'%(random.randint(1,customer_count))).click()

	# 添加会员动作的组合
	def add_customer(self,driver,customerphone,customername,childdate,creditkids,creditcloth):
		self.input_phone(driver,customerphone)
		self.input_name(driver,customername)
		self.select_sex(driver)
		self.input_date(driver,childdate)
		self.input_creditkids(driver,creditkids)
		self.input_creditcloth(driver,creditcloth)
		self.click_button(driver)

	# 随机编辑一条记录
	def edit_customer(self,driver,path,customerphone,edit_data):
		# 1.登录；2.打开会员管理页面；3.输入电话号码（为空）,点击查询；4.找到刚才定义的元素
		Utility.miss_login(driver,path)
		driver.find_element_by_link_text('会员管理').click()
		self.click_query_button(driver,customerphone)
		# 编写sql，查询符合条件的记录数
		sql = 'select count(customerid) from customer'
		customer_count = Utility.query_one(path,sql)[0]
		self.get_random_edit_ele(driver,customer_count)

if __name__ == '__main__':

    driver = webdriver.Firefox()
    Customer().edit_customer(driver,'..\\config\\base.conf','')
